//
//  MainViewController.m
//  chargeAPP
//
//  Created by admin on 2017/3/28.
//  Copyright © 2017年 CarCharge. All rights reserved.
//

#import "MainViewController.h"
#import "BaseNavigationViewController.h"
#import "VehicleDataViewController.h"
#import "SingleDayViewController.h"
#import "ManyDaysViewController.h"
#import "UIImage+Color.h"

@interface MainViewController ()<UITabBarControllerDelegate>


//@property (nonatomic,strong)UIButton* scanButton;
@property (nonatomic,assign)BOOL allowJump;


 @end

@implementation MainViewController

//有且只会调用一次
+ (void)load
{
    //1.设置按钮选中标题的颜色
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
    [item setTitleTextAttributes:@{
                                   NSForegroundColorAttributeName:[UIColor colorWithRed:0.21f green:0.50f blue:0.90f alpha:1.00f]
                                   } forState:UIControlStateSelected];
    //2.设置字体尺寸
    [item setTitleTextAttributes:@{
                                   NSFontAttributeName:[UIFont systemFontOfSize:12]
                                   } forState:UIControlStateNormal];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //去掉tabBar顶部线条
    CGRect rect = CGRectMake(0, 0, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
    
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
    
    self.tabBar.translucent = NO;
    self.delegate = self;
    [self creatChiledViewController];
    [self setupCustomTabBar];
    _allowJump = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpProhibit) name:@"jumpProhibit" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(allowProhibit) name:@"allowProhibit" object:nil];
    
    if ([UINavigationBar instancesRespondToSelector:@selector(setShadowImage:)])
        
    {
        
        [[UINavigationBar appearance] setShadowImage:[UIImage imageWithColor:[UIColor clearColor] size:CGSizeMake(320, 3)]];
        
    }
    
}
- (void)allowProhibit
{
    _allowJump = YES;
}
- (void)jumpProhibit
{
    _allowJump = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    

//    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"roundBtn"]];
//    imageView.frame = CGRectMake(0, 0, kScreenWidth, NavationBarHeight);
//    [self.navigationController.view addSubview:imageView];
//
    if ([[[UIDevice currentDevice] systemVersion] integerValue]>=9) {
        UIImage *image = [[UIImage imageNamed:@"round"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
        [[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }else{
        UIImage *image = [[UIImage imageNamed:@"roundBtn"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
        [[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }
}



- (void)creatChiledViewController
{
    
    VehicleDataViewController *vehicleDataVC= [VehicleDataViewController new];
    BaseNavigationViewController *vehicleDataNV = [[BaseNavigationViewController alloc] initWithRootViewController:vehicleDataVC];
    [self addChildViewController:vehicleDataNV];
    
    SingleDayViewController *singleDayVC= [SingleDayViewController new];
    BaseNavigationViewController *singleDayNV = [[BaseNavigationViewController alloc] initWithRootViewController:singleDayVC];
    
    [self addChildViewController:singleDayNV];
    
    
    ManyDaysViewController *manyDaysVC= [ManyDaysViewController new];
    
    BaseNavigationViewController *manyDaysNV = [[BaseNavigationViewController alloc] initWithRootViewController:manyDaysVC];
    [self addChildViewController:manyDaysNV];



}
- (void)setupCustomTabBar
{
    BaseNavigationViewController *vehicleDataNV = self.childViewControllers[0];
    vehicleDataNV.title = @"车辆数据";
    vehicleDataNV.tabBarItem.image = [[UIImage imageNamed:@"carNormal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vehicleDataNV.tabBarItem.selectedImage = [[UIImage imageNamed:@"carsel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    
    BaseNavigationViewController *singleDayNV = self.childViewControllers[1];
    singleDayNV.title = @"单日统计";
    singleDayNV.tabBarItem.image = [[UIImage imageNamed:@"calendaNormal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    singleDayNV.tabBarItem.selectedImage = [[UIImage imageNamed:@"calendarSel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    BaseNavigationViewController *manyDaysNV = self.childViewControllers[2];
    manyDaysNV.title = @"多日统计";
    manyDaysNV.tabBarItem.image = [[UIImage imageNamed:@"statisticalNormal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    manyDaysNV.tabBarItem.selectedImage = [[UIImage imageNamed:@"statisticalSel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    

}



-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    
    if ([item.title isEqualToString:@""]) {

        
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if(_allowJump){
        return YES;
    }else {
        return NO;
    }
}



@end
